from os import name, stat_result
import sys
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow
import requests
from ui_main import Ui_MainWindow
from PyQt5.QtGui import QIntValidator
from login import LoginWindow
from publish import PublishWindow
from PersonalItems import PersonalItemsWindow
import json
from network import Network


class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, username, parent=None):
        super(MainWindow, self).__init__(parent=parent)
        self.username = username
        self.setupUi(self)
        self.publishBtn.clicked.connect(self.publish)
        self.buyButton.clicked.connect(self.buy)
        self.refrshBtn.clicked.connect(self.refresh)
        self.personal_infoButton.clicked.connect(self.show_personal_items)
        self.goodsList.itemClicked.connect(self.info)

    def publish(self):
        self.Publish = PublishWindow(self.username)
        self.Publish.show()

    def buy(self):
        print("buy")

    def refresh(self):
        self.goodsList.clear()
        goodsList = Network.getGoods(self.keywordLine.text())
        self.goodsList.goodsList = goodsList
        for good in goodsList:
            self.goodsList.addItem(good['name'])

    def info(self,item):
        for goods in self.goodsList.goodsList:
            if goods['name'] == item.text():
                self.nameLabel.setText(goods['name'])
                self.ownerLabel.setText(goods['ower'])
                self.typeLabel.setText(goods['goodsType'])
                self.statusLabel.setText(goods['status'])
                self.describeLabel.setText(goods['describe'])

    def show_personal_items(self):
        self.personal_items = PersonalItemsWindow(self.username)
        self.personal_items.show()



